import 'package:flutter/material.dart';
import 'package:flutter_default_project/models.dart';
import 'package:flutter_default_project/typedefs.dart';

import 'package:todos_app_core/todos_app_core.dart';

import '../widgets/stat.dart';
import '../widgets/todo_list.dart';

class HomeScreen extends StatefulWidget {
  final AppState appState;
  final TodoUpdater todoUpdater;
  final VoidCallback removeComplete;
  const HomeScreen({
    super.key,
    required this.appState,
    required this.todoUpdater,
    required this.removeComplete,
  });

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  VisibilityFilter _filter = VisibilityFilter.all;
  int pageIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
        actions: [
          PopupMenuButton(
            onSelected: toggleHandle,
            itemBuilder: (_) => [
              PopupMenuItem(
                value: ExtraAction.toggleAllComplete,
                child: const Text('toggle all'),
              ),
              PopupMenuItem(
                value: ExtraAction.clearCompleted,
                child: const Text('clear completed'),
              ),
            ],
            child: Icon(Icons.drag_handle),
          ),
          PopupMenuButton(
            onSelected: (value) {
              setState(() {
                _filter = value;
              });
            },
            itemBuilder: (_) => [
              PopupMenuItem(
                value: VisibilityFilter.all,
                child: const Text('all'),
              ),
              PopupMenuItem(
                value: VisibilityFilter.active,
                child: const Text('active'),
              ),
              PopupMenuItem(
                value: VisibilityFilter.completed,
                child: const Text('completed'),
              ),
            ],
          ),
        ],
      ),
      body: pageIndex == 0
          ? TodoList(
              todos: widget.appState.todos.where((item) {
                switch (_filter) {
                  case VisibilityFilter.all:
                    return true;
                  case VisibilityFilter.active:
                    return !item.complete;
                  case VisibilityFilter.completed:
                    return item.complete;
                }
              }).toList(),
              updateTodo: widget.todoUpdater,
            )
          : Stat(appState: widget.appState),
      bottomNavigationBar: BottomNavigationBar(
        onTap: (value) => {
          setState(() {
            pageIndex = value;
          }),
        },
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
          BottomNavigationBarItem(icon: Icon(Icons.star_rate), label: 'stat'),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context).pushNamed(ArchSampleRoutes.addTodo);
        },
        child: Icon(Icons.add),
      ),
    );
  }

  void toggleHandle(ExtraAction value) {
    switch (value) {
      case ExtraAction.toggleAllComplete:
        widget.appState.todos.forEach((todo) {
          widget.todoUpdater(todo, complete: true);
        });
        break;
      case ExtraAction.clearCompleted:
        // todoUpdater(appState.todos.where((todo) => !todo.complete).toList());
        widget.removeComplete();
        break;
    }
  }
}
